package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.*;
import com.mbyte.easy.admin.service.IVideoService;
import com.mbyte.easy.common.constant.StatusRecordVideo;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-12
 */
@Slf4j
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    @Resource VideoMapper videoMapper;

    @Resource  SysUserMapper sysUserMapper;

    @Resource GroupMapper groupMapper;

    @Resource  RelationMapper relationMapper;
    @Resource CheckMapper checkMapper;
    @Resource AllotMapper allotMapper;

    @Resource AudioMapper audioMapper;

    /**
     * 检查状态码 并改变状态码
     * @param code 达到要求的状态码
     * @param changCode  如果一组都是通过后需要改变后状态码
     */
    public  void  checkSttusCode(Integer code,Integer changCode){
        //按组的编号 进行分组
        QueryWrapper<Video>queryWrapper=new QueryWrapper<>();
        queryWrapper.groupBy("group_id");
        List<Video> selectList = videoMapper.selectList(queryWrapper);
        for(Video video:selectList){
            // 对每一组的进行操作
            QueryWrapper<Video>videoQueryWrapper=new QueryWrapper<>();
            videoQueryWrapper.eq("group_id",video.getGroupId());
            List<Video> videoList = videoMapper.selectList(videoQueryWrapper);
                //检查状态码是不是一致并且都是 code
                Boolean allOk=true;
                for(Video video1:videoList){
                    allOk=false;
                   if(code.equals(video1.getStatusCode())){
                       allOk=true;
                   }
                }
                //这一组都一样的状态码 统一更改状态码
                if(allOk){
                   for(Video video1:videoList){
                       video1.setStatusCode(changCode);
                       videoMapper.updateById(video1);
                   }
                }
        }
    }

    @Override
    public Long selectUserIdByUsername(String currentUsername) {
        return sysUserMapper.selectUserIdByUsername(currentUsername);
    }

    @Override
    public Long selectGroupIdByUserId(Long currentUserId) {
        return groupMapper.selectGroupIdByUserId(currentUserId);
    }
/**
 *@author 孙祺至
 *@date 2019/12/20 23:54
 */
    @Override
    public IPage leaderCheckIndex(Video video, Integer pageNo, Integer pageSize) {
        log.info("抽查 draft --> {}",video.toString());
//        List<SysUser> checkerList = relationService.allotBefore();//获取自己的组员
        QueryWrapper<Video> qw = new QueryWrapper<>();
//        qw.in("group_id", checkerList.stream().map(SysUser::getId));//获取所有自己分配的稿件
        //按自己的组ID查看、审核
        qw.eq("group_id",video.getGroupId());
        //PPT录屏是视频校对人通过的
        qw.eq("status_code", StatusRecordVideo.STATUS_VIDEO_AUDIOREADER_SUCCESS)//1023
            .or().eq("status_code",StatusRecordVideo.STATUS_VIDEO_RECORD_AUVELEADER_SUCCESS);//1024
        IPage<Video> videoIPage = videoMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        log.info("抽查 draftList --> {}",videoIPage.getRecords());
        return videoIPage;
    }

    @Override
    public Map setGroupStatus() {
        Map<Integer,String> status = new HashMap<>();
        //按组ID分
        List<Relation>RelationList=relationMapper.selectByAllotId(sysUserMapper.selectByUsername(Utility.getCurrentUsername()).getId());
        log.info("========下属ID==================>>>>{}",RelationList);
        //一个时6一个时10
        List<Group> groupByUserId=new ArrayList<>();
        for (int i = 0; i < RelationList.size(); i++) {
            List<Group> groupByUserId1 = groupMapper.getGroupByUserId(RelationList.get(i).getUserId());
            if (groupByUserId1.size()!=0){
                for (int i1 = 0; i1 < groupByUserId1.size(); i1++) {
                    groupByUserId.add(groupByUserId1.get(i1));
                }
            }
        }
        log.info("==========所有的group==========>>>>>>{}",groupByUserId);
        for (Integer i = 0; i < groupByUserId.size(); i++) {
            status.put(i,groupByUserId.get(i).getGroupId());
        }
        log.info("=========statusMap==========>>>>>{}",status);
        return status;
    }

    @Override
    public int insertCheck(Check check) {
        return checkMapper.insert(check);
    }

    @Override
    public List<Allot> getAllotByAllotId(Long selectUserIdByUsername) {
        return allotMapper.selectByAllotId(selectUserIdByUsername);
    }

    @Override
    public Video selectById(Long fileId) {
        return videoMapper.selectById(fileId);
    }

    @Override
    public Audio GetGroupIdById(Long id) { return audioMapper.selectById(id); }

    @Override
    public SysUser getUserByUserName(String username) { return sysUserMapper.selectByUsername(username); }

    @Override
    public List<Allot> getAllotDraftIdByUserId(Long userId) {
        return allotMapper.getAllotDraftIdByUserId(userId);
    }

    @Override
    public Video selectAGroupIdById(Long id) { return videoMapper.selectById(id);}

    @Override
    public IPage<Video> listPage(IPage<Video> page, QueryWrapper<Video> queryWrapper) {
        return page.setRecords(this.baseMapper.listPage(page,queryWrapper));
    }

    @Override
    public IPage<Video> listPagess(IPage<Video> page, QueryWrapper<Video> queryWrapper) {
        return page.setRecords(this.baseMapper.listPage(page,queryWrapper));
    }

}
